/**
 * 
 */
package com.xwtech.cring.admin.bo;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.xwtech.cring.pub.dao.SysUserInfoDAO;
import com.xwtech.cring.pub.dao.SysUserRoleDAO;
import com.xwtech.cring.pub.po.SysUserInfo;
import com.xwtech.framework.pub.dao.FrameLoginDAO;
import com.xwtech.framework.pub.po.FrameLogin;
import com.xwtech.framework.pub.result.ResultConstants;
import com.xwtech.framework.pub.result.ResultInfo;
import com.xwtech.framework.pub.result.ResultInfos;
import com.xwtech.framework.pub.utils.DateUtils;
import com.xwtech.framework.pub.web.FrameworkApplication;
import com.xwtech.framework.pub.web.SystemConstants;

/**
 * @author Administrator
 * 
 */
public class BgUserBO {

	private SysUserInfoDAO sysUserInfoDAO = FrameworkApplication.cringDbCollection
			.getSysUserInfoDAO();

	private FrameLoginDAO frameLoginDAO = FrameworkApplication.cringDbCollection
			.getFrameLoginDAO();

	private TransactionTemplate transTemplate = FrameworkApplication.cringDbCollection
			.getTransTemplate();

	private SysUserRoleDAO sysUserRoleDAO = FrameworkApplication.cringDbCollection
			.getSysUserRoleDAO();

	public BgUserBO() {

	}

	/**
	 * 根据登陆名获取用户信息 先根据登陆名获取登陆信息，再根据登陆信息获取用户信息
	 * 
	 * @param loginName
	 * @return
	 */
	public SysUserInfo getBgUserByLoginname(String loginName) {
		FrameLogin frameLogin = frameLoginDAO
				.queryFrameLoginByLoginName(loginName);
		SysUserInfo sysUserInfo = sysUserInfoDAO
				.querySysUserInfoByLoginId(frameLogin.getLoginId());

		return sysUserInfo;
	}

	/**
	 * 根据关键字来删除后台用户信息 需要删除系统用户表和登陆表 用户角色表信息 三者在同一个事务之内
	 * 
	 * @param resultInfos
	 *            ResultInfos 返回信息
	 * @param strUserInfoId
	 *            String 系统用户标识字符串
	 * 
	 * @throws Exception
	 *             抛出异常
	 */
	public void deleteBgUser(final ResultInfos resultInfos,
			final String loginName) throws Exception {
		this.transTemplate
				.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		this.transTemplate.execute(new TransactionCallbackWithoutResult() {
			public void doInTransactionWithoutResult(TransactionStatus status) {
				if (loginName != null && loginName.length() > 0) {

					FrameLogin frameLogin = frameLoginDAO
							.queryFrameLoginByLoginName(loginName);

					SysUserInfo sysUserInfo = sysUserInfoDAO
							.querySysUserInfoByLoginId(frameLogin.getLoginId());

					sysUserInfoDAO
							.executeHSQL("update SysUserInfo set status='"
									+ SystemConstants.STATUS_INVALID
									+ "',endTime='" + DateUtils.getChar14()
									+ "' where userInfoId="
									+ sysUserInfo.getUserInfoId());
					sysUserInfoDAO.getHibernateTemplate().flush();
					sysUserInfoDAO
							.executeHSQL("update FrameLogin set loginState='"
									+ SystemConstants.STATUS_INVALID
									+ "' where loginId="
									+ sysUserInfo.getLoginId());

					sysUserRoleDAO.deleteSysUserRoleByUserId(sysUserInfo
							.getUserInfoId());

				} else
					resultInfos
							.add(new ResultInfo(
									ResultConstants.ADMIN_SYSUSERINFO_DELETE_NO_RECORD));
			}
		});
		resultInfos.add(new ResultInfo(
				ResultConstants.ADMIN_SYSCONFIG_DELETE_SUCCESS));
	}

	public FrameLogin getFrameLogin(String loginName) {
		FrameLogin frameLogin = frameLoginDAO
				.queryFrameLoginByLoginName(loginName);
		return frameLogin;
	}
}
